/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	RBSystemPromptProvider.java
 * 模块说明：	
 * 修改历史：
 * Nov 6, 2007 - lxm - 创建。
 */
package com.hd123.rumba.prompt.provider;

import java.text.ParseException;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.hd123.rumba.cartridge.biz.BCartFunctionView;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.prompt.IPrompt;
import com.hd123.rumba.prompt.IPromptMgr;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.user.IRole;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.util.ObjectUtil;
import com.hd123.rumba.util.XMLUtil;

/**
 * rumba系统提醒提供者 | 实现类
 * 
 * @author lxm
 * 
 */
public class RBSystemPromptProvider implements IPromptProvider {

  public BCartFunctionView getCFV() {
    return null;
  }

  public String getCaption() {
    return "系统提醒";
  }

  public IPromptContent createContent() throws BusinessException {
    return new RBPromptContent();
  }

  public IPromptContent createContent(String context) throws BusinessException {
    RBPromptContent content = new RBPromptContent();
    if (context == null)
      return content;
    Document doc = XMLUtil.parse(context);

    NodeList nlContent = doc.getElementsByTagName("promptContent");
    for (int i = 0; i < nlContent.getLength() - 1; i++) {
      Node nContent = nlContent.item(i);
      if (!(nContent instanceof Element))
        continue;
      Element eContent = (Element) nContent;

      NodeList nlKey = eContent.getChildNodes();
      for (int j = 0; j < nlKey.getLength(); j++) {
        Node nKey = nlKey.item(j);
        if (!(nKey instanceof Element))
          continue;
        Element eKey = (Element) nKey;

        String key = eKey.getTagName();
        Integer type = XMLUtil.readInteger(eKey, "type");
        Object value = null;
        if (RBPromptContent.Type.ISNULL.equals(type))
          value = null;
        else if (RBPromptContent.Type.STRING.getValue().equals(type))
          value = XMLUtil.readString(eKey, "value");
        else if (RBPromptContent.Type.INTEGER.getValue().equals(type))
          value = XMLUtil.readInteger(eKey, "value");
        else if (RBPromptContent.Type.BIGDECIMAL.getValue().equals(type))
          value = XMLUtil.readBigDecimal(eKey, "value");
        else if (RBPromptContent.Type.DATE.getValue().equals(type))
          try {
            value = XMLUtil.readDate(eKey, "value");
          } catch (ParseException e) {
            throw new BusinessException(e);
          }
        else if (RBPromptContent.Type.BOOLEAN.getValue().equals(type))
          value = XMLUtil.readBoolean(eKey, "value");
        content.put(key, value);
      }
    }
    return content;
  }

  public void putToRoleT(Object receiver, IPromptContent content, OperateContext operate)
      throws BusinessException, HibernateException {
    ObjectUtil.checkParameterNotNull(receiver, "receiver");
    ObjectUtil.checkParameterNotNull(content, "content");
    ObjectUtil.checkParameterNotNull(operate, "operate");
    assert receiver instanceof IRole;
    IRole role = (IRole) receiver;

    IPrompt prompt = this.getPmptMgr().createPrompt(role.getDomain());
    prompt.setReceiverRole(role);
    this.writePromptContent(prompt, content);

    Session ses = HibernateSession.getSession();
    Transaction tx = ses.beginTransaction();
    try {
      this.getPmptMgr().save(ses, prompt, operate);
      tx.commit();
    } catch (BusinessException e) {
      tx.rollback();
      throw e;
    } catch (HibernateException e) {
      tx.rollback();
      throw e;
    } finally {
      ses.flush();
    }
  }

  public void putToUserT(Object receiver, IPromptContent content, OperateContext operate)
      throws BusinessException, HibernateException {
    ObjectUtil.checkParameterNotNull(receiver, "receiver");
    ObjectUtil.checkParameterNotNull(content, "content");
    ObjectUtil.checkParameterNotNull(operate, "operate");
    assert receiver instanceof IUser;
    IUser user = (IUser) receiver;

    IPrompt prompt = this.getPmptMgr().createPrompt(user.getDomain());
    prompt.setReceiverUser(user);
    this.writePromptContent(prompt, content);

    Session ses = HibernateSession.getSession();
    Transaction tx = ses.beginTransaction();
    try {
      this.getPmptMgr().save(ses, prompt, operate);
      tx.commit();
    } catch (BusinessException e) {
      tx.rollback();
      throw e;
    } catch (HibernateException e) {
      tx.rollback();
      throw e;
    } finally {
      ses.flush();
    }
  }

  private IPromptMgr pmptMgr = null;

  private IPromptMgr getPmptMgr() throws BusinessException {
    if (pmptMgr == null)
      pmptMgr = (IPromptMgr) NamingMgr.getInstance().lookupMgr(IPromptMgr.class);
    return pmptMgr;
  }

  private void writePromptContent(IPrompt prompt, IPromptContent content) throws BusinessException {
    prompt.setSenderId(content.getSenderId());
    prompt.setSenderClassName(content.getSenderClassName());
    prompt.setLevel(content.getLevel());
    prompt.setMessage(content.getTitle());
    prompt.setText(content.getText());
    prompt.setFlag(content.getFlag());
    prompt.setTime(content.getTime());
    if (content instanceof IPromptContentWeb) {
      prompt.setLink(((IPromptContentWeb) content).getLink());
      prompt.setLinkPerm(((IPromptContentWeb) content).getLinkPerm());
    }
  }
}
